import { defineStore } from 'pinia'

// 认证存储模块
export const useAuthStore = defineStore('auth', {
  state: () => ({
    user: null,
    permissions: [],
    menus: [],
    isAuthenticated: false
  }),
  getters: {
    getUser: (state) => state.user,
    getPermissions: (state) => state.permissions,
    hasPermission: (state) => (code) =>
      state.user?.is_superuser || state.permissions.includes(code),
    getMenus: (state) => state.menus
  },
  actions: {
    setUser(user) {
      this.user = user
      this.isAuthenticated = !!user
    },
    setPermissions(permissions) {
      this.permissions = permissions
    },
    setMenus(menus) {
      this.menus = menus
    },
    logout() {
      this.$reset()
      localStorage.removeItem('access_token')
      localStorage.removeItem('user_info')
    }
  }
})
